package com.example.service;


import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import scala.collection.generic.BitOperations.Int;

import com.example.Mapper.CgRkDao;
import com.example.pojo.Belaidup;
import com.example.pojo.Belaidupcong;
import com.example.pojo.oderfrom;

//采购入库
@Service
public class CgRkService {

	@Autowired
	//采购入库dao层
private CgRkDao mapper;

	
	//返回list集合
	public Belaidup list(int page){
		Belaidup w=mapper.List( page);
	List<Belaidupcong>	 b=mapper.cList(w.getBillNO());
		w.setList1(b);
		return w;
		
	}
	
	//新增
	@Transactional
	public void Insert(Belaidup b){
		String billno=null;
		System.out.println(b.getBillNO());
		mapper.Delete(b.getBillNO());//删除从表
		mapper.Deletezhu(b.getBillNO());//删除主表
	
		mapper.Insertrk(b);//新增主表
	
		for (Belaidupcong b1 : b.getList1()) {
			billno=b1.getBillNo();
			b1.setPk_id(b.getBillNO());
		if(billno!=null){
			mapper.updatedd1(b1);//修改采购订单从表的 未入库量
		}
			mapper.Insertrkxx(b1);//新增从表
		}
		
	
		
	}
	//查询主表记录数
	public int SelectCount(){
		return mapper.selectCount();
		
		
	}
	//修改入库主表为已审核
	public void update1(Belaidup b){
		String billno=null;

		Boolean shifou=true;
		for (Belaidupcong b1 : b.getList1()) {
			billno=b1.getBillNo();
			
			mapper.Updatejy(b1);
		}
		//判断采购订单是否全部出库
		if(billno!=null){
		for (Belaidupcong b2  : mapper.selectdd(billno)) {
			if(Integer.parseInt( b2.getShuliang())>0){
				shifou=false;
			}
		}
		//修改采购订单状态
		if(shifou){
			mapper.updatedd2(billno);
		}
		mapper.upadte1(b.getBillNO());
		}	
	}
	//修改入库主表为未审核
	public void update2(String id){
		mapper.upadte2(id);
		
	}
	//删除主表
		public void update3(String id){
			mapper.upadte3(id);
			
		}
	
	//转单加载从表
	public Belaidup findor1( String no){
		Belaidup b=mapper.cList1(no);
		b.setZhuangtai("未审核");
		b.setWareid("北京仓");
		List<Belaidupcong> l=mapper.cList2(no);
		for (Belaidupcong o1 : l) {
		if(mapper.Selectjy(o1)==0){
		for (Belaidupcong o : l) {
			o.setDanbie("采购订单");
			mapper.Insertjy(o);
			
		}
		}
		}
		b.setList1(l);
		return b;
	}
	 //采购订单转单查询
	public List<oderfrom>  findorder( String time){
		return mapper.findorder(time);
	}
	
}
